package cn.imvc.app.models;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.imvc.app.entities.Address;
import cn.imvc.app.entities.Area;
import cn.imvc.app.entities.Member;
import cn.imvc.app.repositories.AddressRepository;
import cn.imvc.core.ICache;

@Service
public class AddressModelImpl implements AddressModel {

	@Autowired
	private AddressRepository addressRepository;

	@Autowired
	DealerApplyModel dealerApplyModel;

	@Autowired
	MemberModel memberModel;

	@Autowired
	AreaModel areaModel;

	@Autowired
	ICache cache;

	public Address get(int id) {
		return addressRepository.findOne(id);
	}

	public Address save(Address address) {
		Address account2 = addressRepository.saveAndFlush(address);
		return account2;
	}

	public Address update(Address newAddress) {
		Address loadAddress = get(newAddress.getId());
		BeanUtils.copyProperties(newAddress, loadAddress, new String[] { "id" });
		return addressRepository.saveAndFlush(loadAddress);
	}

	public Map<String, String> delete(Member member, int id) {
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", "0");
		if (member == null) {
			result.put("code", "-1");
			result.put("msg", "请登录");
			return result;
		}

		Address address = addressRepository.findByMemberAndId(member, id);
		if (address == null) {
			result.put("code", "1");
			result.put("msg", "您没有权限操作");
			return result;
		}
		if (address.getIsDefault() == 1) {
			result.put("code", "0");
			result.put("msg", "默认地址无法删除");
		}
		addressRepository.delete(id);
		result.put("code", "1");
		result.put("msg", "删除成功");
		return result;
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			addressRepository.delete(id);
		}
	}

	public List<Address> findByMemberId(int memberid) {
		return addressRepository.findByMemberIdOrderByIsDefaultDesc(memberid);
	}

	public Map<String, String> saveAddress(Member member, Address address, String area) {
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", "0");
		if (member == null) {
			result.put("code", "-1");
			result.put("msg", "请登录");
			return result;
		}
		if(StringUtils.isEmpty(address.getName())){
			result.put("msg", "收货人姓名不能为空");
			return result;
		}
		if(StringUtils.isEmpty(address.getMobPhone())){
			result.put("msg", "收货人电话不能为空");
			return result;
		}
		if(StringUtils.isEmpty(area)){
			result.put("msg", "收货人所属地区不能为空");
			return result;
		}
		if(StringUtils.isEmpty(address.getAddress())){
			result.put("msg", "详细地址不能为空");
			return result;
		}
		address = setDefault(member, address);
		address = setAreas(address, area);
		address.setMember(member);
		addressRepository.saveAndFlush(address);
		result.put("code", "1");
		result.put("msg", "添加成功");
		return result;
	}

	public Map<String, String> updateAddress(Member member, Address address, String area) {
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", "0");
		if (member == null) {
			result.put("code", "-1");
			result.put("msg", "请登录");
			return result;
		}
		if(StringUtils.isEmpty(address.getId())){
			result.put("code", "1");
			result.put("msg", "数据不存在");
			return result;
		}
		Address findAddress = addressRepository.findByMemberAndId(member, address.getId());
		if (findAddress == null) {
			result.put("msg", "您没有权限");
			return result;
		}
		if(StringUtils.isEmpty(address.getName())){
			result.put("msg", "收货人姓名不能为空");
			return result;
		}
		if(StringUtils.isEmpty(address.getMobPhone())){
			result.put("msg", "收货人电话不能为空");
			return result;
		}
		if(StringUtils.isEmpty(area)){
			result.put("msg", "收货人所属地区不能为空");
			return result;
		}
		if(StringUtils.isEmpty(address.getAddress())){
			result.put("msg", "详细地址不能为空");
			return result;
		}
		// 本身是默认地址的不能修改
		if(findAddress.getIsDefault() == (byte)1){
			address.setIsDefault((byte)1);
		}
		address = setDefault(member, address);
		address = setAreas(address, area);
		address.setMember(member);
		addressRepository.saveAndFlush(address);
		result.put("code", "1");
		result.put("msg", "修改成功");
		return result;
	}

	public Map<String, String> setDefault(Member member, int addressid) {
		Map<String, String> result = new HashMap<String, String>();
		result.put("code", "0");
		if (member == null) {
			result.put("code", "-1");
			result.put("msg", "请登录");
			return result;
		}
		Address findAddress = addressRepository.findByMemberAndId(member, addressid);
		if(findAddress == null){
			result.put("code", "0");
			result.put("msg", "您没有权限操作");
			return result;
		}
		Address addr = new Address();
		List<Address> list = addressRepository.findByIsDefaultAndMember((byte) 1, member);
		for (int i = 0; i < list.size(); i++) {
			addr = list.get(i);
			addr.setIsDefault((byte) 0);
			addressRepository.saveAndFlush(addr);
		}
		Address address = addressRepository.getOne(addressid);
		address.setIsDefault((byte) 1);
		addressRepository.saveAndFlush(address);
		result.put("code", "1");
		result.put("msg", "设置成功");
		return result;
	}

	private Address setAreas(Address address, String area) {
		String[] areas = area.split(" ");
		if (areas.length >= 1) {
			Area province = areaModel.get(areaModel.findByName(areas[0]).get(0).getId());
			address.setProvince(province);
			if (areas.length >= 2) {
				Area city = areaModel.get(areaModel.findByName(areas[1], province).get(0).getId());
				address.setCity(city);
				if (areas.length >= 3) {
					Area county = areaModel.get(areaModel.findByName(areas[2], city).get(0).getId());
					address.setCounty(county);
				}
			}
		}
		return address;
	}

	private Address setDefault(Member member, Address address) {
		Address addr = new Address();
		if (address.getIsDefault() == (byte)1) {
			List<Address> list = addressRepository.findByIsDefaultAndMember((byte) 1, member);
			for (int i = 0; i < list.size(); i++) {
				addr = list.get(i);
				addr.setIsDefault((byte) 0);
				addressRepository.saveAndFlush(addr);
			}
		}
		List<Address> addressList = addressRepository.findByMember(member);
		if (addressList == null || (addressList != null && addressList.size() == 0)) {
			address.setIsDefault((byte) 1);
		}
		return address;
	}
}
